package com.rosense.client.i;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.rosense.client.rpc.RPCBean;
import com.rosense.client.rpc.RPCIoBean;
import com.rosense.client.rpc.RPCMemBean;
import com.rosense.commons.DateUtils;
import com.rosense.commons.LangUtils;
import com.rosense.commons.NumberUtils;
import com.rosense.commons.StringsUtils;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月24日 下午2:22:21
 * 
 */
public class WRMem extends IWriteReader {

	@Override
	public String getName() {
		return "mem";
	}

	@Override
	public String getText() {
		return "内存";
	}

	@Override
	public void write(String host, RPCBean bean) {
		RPCMemBean memBean = (RPCMemBean) bean;
		StringBuilder sb = new StringBuilder();
		sb.append(System.currentTimeMillis()).append(",");
		sb.append(memBean.used).append(",");
		sb.append(memBean.total).append(",");
		sb.append(memBean.cached);
		append(host, sb.toString());
	}

	@Override
	protected void parserLine(String line, Map<String, String> map) {
		String[] cols = line.split(",");
		RPCMemBean mem = new RPCMemBean();
		mem.time = LangUtils.toLong((cols[0]));
		mem.used = LangUtils.toFloat(cols[1], 0.0f);
		mem.total = LangUtils.toFloat(cols[2], 0.0f);
		try {
			mem.cached = LangUtils.toFloat(cols[3], 0.0f);
		} catch (Exception e) {
		}
		map.put("time", DateUtils.formatDate(new Date(mem.time), "yyyyMMddHHmm"));
		map.put("value", "含缓存使用率:" + NumberUtils.formatNumber("0.0%", mem.used, mem.total) + "," + "不含缓存使用率:" + NumberUtils.formatNumber("0.0%", mem.used - mem.cached, mem.total));
	}
	@Override
	protected void parserLineToMap(String line,Map<String,List<RPCBean>> map) {
		String[] cols = line.split(",");
		RPCMemBean mem = new RPCMemBean();
		mem.time = LangUtils.toLong((cols[0]));
		mem.used = LangUtils.toFloat(cols[1], 0.0f);
		mem.total = LangUtils.toFloat(cols[2], 0.0f);
		try {
			mem.cached = LangUtils.toFloat(cols[3], 0.0f);
		} catch (Exception e) {
		}
		String time = DateUtils.formatDate(new Date(mem.time), "HH");
		List<RPCBean> list = map.get(time);
		if(list==null){
			list = new ArrayList<RPCBean>();
			map.put(time, list);
		}
		list.add(mem);
	}
	@Override
	protected void getAvgInfo(Map<String, List<RPCBean>> mapInfo,Map<String,String> map,double limit){
		
		float total1 = 0;
		float total2 = 0;
		float totalNum = 0;
		String str1 = "";
		String str2 = "";
		String data1 = "";
		String data2 = "";
		String xdata = "";
		int n = 0;
		for(String time : mapInfo.keySet()){
			List<RPCBean> list = mapInfo.get(time);
			totalNum += list.size();
			float value1 = 0;
			float value2 = 0;
			for(int i=0;i<list.size();i++){
				RPCMemBean mem = (RPCMemBean) list.get(i);
				value1 += LangUtils.toFloat(NumberUtils.formatNumber("0.00", mem.used*100, mem.total),0);
				value2 += LangUtils.toFloat(NumberUtils.formatNumber("0.00",  (mem.used - mem.cached)*100, mem.total),0);
			}
			total1 += value1;
			total2 += value2;
			value1 = (float)NumberUtils.formatFmZero(value1,list.size());
			value2 = (float)NumberUtils.formatFmZero(value2,list.size());
			data1 +=value1;
			data2 +=value2;
			xdata +=time;
			if(value1>limit){
				if(StringsUtils.isBlank(str1)){
					str1 += "含缓存:";
				}else{
					str1 += ",";
				}
				str1 += time;
			}
			if(value2>limit){
				if(StringsUtils.isBlank(str2)){
					str2 += "<br/>不含缓存:";
				}else{
					str2 += ",";
				}
				str2 += time;
			}
			if(n++<mapInfo.size()-1){
				data1 += ",";
				data2 += ",";
				xdata += ",";
			}
		}
		if(StringsUtils.isNotBlank(str1)||StringsUtils.isNotBlank(str1)){
			map.put("hour", str1+str2);
			map.put("day", "含缓存使用率："+ NumberUtils.formatNumber("0.00", total1,totalNum)+"%<br/>不含缓存使用率:"+NumberUtils.formatNumber("0.00", total2,totalNum)+"%");
		}
		//历史的折线图
		if(limit<0){
			map.put("title", "内存使用率");
			map.put("name1", "不含缓存");
			map.put("name2", "含缓存");
			map.put("xdata", xdata);
			map.put("data1", data1);
			map.put("data2", data2);
		}
	}
	@Override
	public List<? extends RPCBean> read(String host) {
		List<RPCMemBean> list = new ArrayList<RPCMemBean>();
		String value = readLastLine(host);
		String[] values = value.trim().split("\n");
		for (String line : values) {
			String[] cols = line.split(",");
			RPCMemBean mem = new RPCMemBean();
			mem.time = LangUtils.toLong((cols[0]));
			mem.used = LangUtils.toFloat(cols[1], 0.0f);
			mem.total = LangUtils.toFloat(cols[2], 0.0f);
			try {
				mem.cached = LangUtils.toFloat(cols[3], 0.0f);
			} catch (Exception e) {
			}
			list.add(mem);
		}
		return list;
	}

}
